package com.e3.web.core.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.web.core.model.SuperModel;
import com.e3.web.core.model.SuperQC;

public class ListenerManager<D extends SuperModel> {

	private List<EditorListener<D>> editorListeners = new ArrayList<EditorListener<D>>();

	private List<QueryListener<D>> queryListeners = new ArrayList<QueryListener<D>>();

	private List<GridListener<D>> gridListeners = new ArrayList<GridListener<D>>();

	private List<TreeListener<D>> treeListeners = new ArrayList<TreeListener<D>>();

	public void addEditorListener(EditorListener<D> listener) {
		editorListeners.add(listener);
	}

	public void removeEditorListener(EditorListener<D> listener) {
		editorListeners.remove(listener);
	}

	public void notifyEditorSuccess(D model) {
		for (EditorListener<D> listener : editorListeners) {
			listener.onEditSuccess(model);
		}
	}

	public void addQueryListener(QueryListener<D> listener) {
		queryListeners.add(listener);
	}

	public void removeQueryListener(QueryListener<D> listener) {
		queryListeners.remove(listener);
	}

	public <T extends SuperQC> void notifyQuerySuccess(T qc,List<D> models) {
		for (QueryListener<D> listener : queryListeners) {
			listener.onQuerySuccessed(qc,models);
		}
	}

	public void addGridListener(GridListener<D> listener) {
		gridListeners.add(listener);
	}

	public void removeGridListener(GridListener<D> listener) {
		gridListeners.remove(listener);
	}

	public void notifyGridSelectChanged(D model) {
		for (GridListener<D> listener : gridListeners) {
			listener.onGridSelectionChanged(model);
		}
	}

	public void notifyGridRowDoubleClicked(D model) {
		for (GridListener<D> listener : gridListeners) {
			listener.onGridRowDoubClicked(model);
		}
	}

	public void addTreeListener(TreeListener<D> listener) {
		treeListeners.add(listener);
	}

	public void removeTreeListener(TreeListener<D> listener) {
		treeListeners.remove(listener);
	}

	public void notifyTreeDataSelected(D parentModel, List<D> models) {
		for (TreeListener<D> listener : treeListeners) {
			listener.onTreeDataSelected(parentModel, models);
		}
	}

}
